<script type="text/javascript" src="{$GLOBALS.site_url}/../system/ext/jquery/jquery.tablednd.js"></script>
<script type="text/javascript">
{literal}
	function deleteCountries() {
		if($("input:checked").length > 0) {
			if ( confirm('{/literal}[[Are you sure you want to delete selected countries?]]{literal}') ) 
				submitForm('delete_country');
			else
				return false;
		} 
		else {
			$(function() {
				$("#dialog").dialog('destroy');
				$("#dialog").attr({title: "{/literal}[[Information]]{literal}"});
				$("#dialog").html("{/literal}[[Please select item(s) first]]{literal}").dialog({width: 300});
			});
			return false;
		} 
	}
{/literal}
</script>
<div id="dialog" style="display: none"></div>
{breadcrumbs}[[Countries]]{/breadcrumbs}
<h1><img src="{image}/icons/linedpaper32.png" border="0" alt="" class="titleicon"/> [[Countries]]</h1>
{include file="country_errors.tpl"}
<div style="width:80%; padding: 10px 0">
	<div style="float: left;">
		<a href="{$GLOBALS.site_url}/add-country/" class="grayButton">[[Add Country]]</a>
		<a href="{$GLOBALS.site_url}/import-countries/" class="grayButton">[[Import Countries]]</a>
		<a href="#" class="grayButton" onclick="submitForm('save_order');">[[Save Order]]</a>
	</div>
	<div style="text-align: right;">
		<form method="post" action="{$GLOBALS.site_url}/countries/">
		<input type="hidden" name="action" value="change_setting" />
		<input type="hidden" name="sorting_order" id="sorting_order" value="{$sorting_order}" />
		<input type="hidden" name="sorting_field" id="sorting_field" value="{$sorting_field}" />
		<input type="hidden" name="page" id="page" value="{$currentPage}" />
		<input type="hidden" name="items_per_page" id="items_per_page" value="{$items_per_page}" />
		[[Default Country]]
		<select name="default_country" onChange="submit()">
	    	<option value="">[[Select Country]]</option>
		    {foreach from=$countriesForDefault item=country}
		    	<option value="{$country.sid}" {if $settings.default_country == $country.sid} selected="selected"{/if}>[[{$country.country_name}]]</option>
		    {/foreach}
	    </select>
	    </form>
	</div>
</div>
<div class="box" id="displayResults" style="width:80%">
	<form method="post" action="{$GLOBALS.site_url}/countries/" name="resultsForm">
	<input type="hidden" name="sorting_order" id="sorting_order" value="{$sorting_order}" />
	<input type="hidden" name="sorting_field" id="sorting_field" value="{$sorting_field}" />
	<input type="hidden" name="page" id="page" value="{$currentPage}" />
    <div class="box-header">
        <div class="resultsnumber"><strong>{$countCountries}</strong> [[countries]]</div>
        <div class="actionWithSelected">
			<input type="hidden" name="action" id="action_name" value="" />
			[[Actions with Selected]]:
			<input type="submit" value="[[Activate]]" class="grayButton" onclick="submitForm('activate');">
			<input type="submit" value="[[Deactivate]]" class="grayButton" onclick="submitForm('deactivate');">
			<input type="submit" value="[[Delete]]" class="deletebutton" onclick="return deleteCountries();">
        </div>

		<div class="pagination">
           	{foreach from=$pages item=page}
                {if $page == $currentPage}
                    <strong>{$page}</strong>
                {else}
                    {if $page == $totalPages && $currentPage < $totalPages-3} ... {/if}
                    <a href="?page={$page}{if $sorting_field ne null}&amp;sorting_field={$sorting_field}{/if}{if $sorting_order ne null}&amp;sorting_order={$sorting_order}{/if}&amp;items_per_page={$items_per_page}">{$page}</a>
                    {if $page == 1 && $currentPage > 4} ... {/if}
                {/if}
            {/foreach}
        </div>

		<div class="numberPerPage">
			[[per page]]:
			<select id="items_per_page" name="items_per_page" onchange="window.location = '?items_per_page='+this.value+'{if $sorting_field ne null}&amp;sorting_field={$sorting_field}{/if}{if $sorting_order ne null}&amp;sorting_order={$sorting_order}{/if}';" class="perPage">
				<option value="10" {if $items_per_page == 10}selected{/if}>10</option>
				<option value="20" {if $items_per_page == 20}selected{/if}>20</option>
				<option value="50" {if $items_per_page == 50}selected{/if}>50</option>
				<option value="100" {if $items_per_page == 100}selected{/if}>100</option>
			</select>
			<input type="hidden" name="restore" value="1" />
		</div>

    </div>

    <div class="innerpadding">
        <div id="displayResultsTable">
            <table width="100%" id="list_table">
                <thead>
                    <tr>
                        <th><input type="checkbox" id="all_checkboxes_control"></th>
                        <th>[[Country Code]]</th>
                        <th>[[Country Name]]</th>
                        <th>[[Status]]</th>
						<th colspan="2">[[Order]]</th>
                        <th class="actions">[[Actions]]</th>
                    </tr>
                </thead>
                <tbody>
                    {foreach from=$countries item=country name=countries_block}
                        <tr {if !$country.active} style="background: none repeat scroll 0 0 #D8D8D8" {else} class="{cycle values = 'evenrow,oddrow'}" {/if} >
                            <td>
								<input type="checkbox" name="countries[{$country.sid}]" value="1" id="checkbox_{$smarty.foreach.countries_block.iteration}" />
								<input type="hidden" name="item_order[{$country.sid}]" value="1"/>
							</td>
                            <td class="dragHandle">{$country.country_code}</td>
                            <td class="dragHandle">[[{$country.country_name}]]</td>
                            <td>
                                {if $country.active}
                                    [[Active]]
                                {else}
                                    [[Not Active]]
                                {/if}
                            </td>
							<td>
								{if !($currentPage == $totalPages && $smarty.foreach.countries_block.iteration == $smarty.foreach.countries_block.total)}
									<a href="?action=move_down&amp;sid={$country.sid}&amp;items_per_page={$items_per_page}&amp;page={$currentPage}">
										<img src="{image}b_down_arrow.gif" border="0" alt=""/></a>
								{/if}
							</td>
							<td>
								{if !($currentPage == 1 && $smarty.foreach.countries_block.iteration == 1)}
									<a href="?action=move_up&amp;sid={$country.sid}&amp;items_per_page={$items_per_page}&amp;page={$currentPage}">
										<img src="{image}b_up_arrow.gif" border="0" alt="" /></a>
								{/if}
							</td>
                            <td>
								<a href="{$GLOBALS.site_url}/edit-country/?country_id={$country.sid}" title="Edit" class="editbutton">[[Edit]]</a>
								<a href="{$GLOBALS.site_url}/countries/?action=delete_country&amp;countries[{$country.sid}]=1&amp;items_per_page={$items_per_page}&amp;page={$currentPage}"
								   onclick="return confirm('[[Are you sure you want to delete this country?]]')" title="Delete" class="deletebutton">[[Delete]]</a>
                            	{if $country.active}
                            		<a href="{$GLOBALS.site_url}/countries/?action=deactivate&amp;countries[{$country.sid}]=1&amp;items_per_page={$items_per_page}&amp;page={$currentPage}" title="Deactivate" class="editbutton">[[Deactivate]]</a>
                            	{else}
                                    <a href="{$GLOBALS.site_url}/countries/?action=activate&amp;countries[{$country.sid}]=1&amp;items_per_page={$items_per_page}&amp;page={$currentPage}" title="Activate" class="editbutton">[[Activate]]</a>
                                {/if}
                            </td>
                        </tr>
                    {/foreach}
                </tbody>
            </table>
        </div>
    </div>
    <div class="box-footer">
        <div class="actionWithSelected">
               [[Actions with Selected]]:
               <input type="submit" value="[[Activate]]" class="grayButton" onclick="submitForm('activate');" />
               <input type="submit" value="[[Deactivate]]" class="grayButton" onclick="submitForm('deactivate');" />
               <input type="submit" value="[[Delete]]" class="deletebutton" onclick="return deleteCountries();" />
        </div>
      
        <div class="pagination">
            {foreach from=$pages item=page}
                {if $page == $currentPage}
                    <strong>{$page}</strong>
                {else}
                    {if $page == $totalPages && $currentPage < $totalPages-3} ... {/if}
                    <a href="?page={$page}{if $sorting_field ne null}&amp;sorting_field={$sorting_field}{/if}{if $sorting_order ne null}&amp;sorting_order={$sorting_order}{/if}&amp;items_per_page={$items_per_page}">{$page}</a>
                    {if $page == 1 && $currentPage > 4} ... {/if}
                {/if}
            {/foreach}
       	</div>
       	
       	<div class="numberPerPage">
			[[per page]]:
			<select id="items_per_page" name="items_per_page" onchange="window.location = '?items_per_page='+this.value+'{if $sorting_field ne null}&amp;sorting_field={$sorting_field}{/if}{if $sorting_order ne null}&amp;sorting_order={$sorting_order}{/if}';" class="perPage">
				<option value="10" {if $items_per_page == 10}selected{/if}>10</option>
				<option value="20" {if $items_per_page == 20}selected{/if}>20</option>
				<option value="50" {if $items_per_page == 50}selected{/if}>50</option>
				<option value="100" {if $items_per_page == 100}selected{/if}>100</option>
			</select>
			<input type="hidden" name="restore" value="1" />
		</div>
    </div>
    </form>
</div>
<script type="text/javascript">
    var total={if $smarty.foreach.countries_block.total}{$smarty.foreach.countries_block.total}{else}0{/if};
    {literal}
    function set_checkbox(param) {
        for (i = 1; i <= total; i++) {
            if (checkbox = document.getElementById('checkbox_' + i)) {
                checkbox.checked = param;
            }
        }
    }

    $("#all_checkboxes_control").click(function() {
        if (this.checked == false){
            set_checkbox(false);
            $("tr[id^='users']").removeAttr('style');
        } else {
            set_checkbox(true);
            $("tr[id^='users']").attr( 'style','background-color: #ffcc99' );
        }
    });

	// Drag'n'Drop table
	$("#list_table").tableDnD({
		onDragClass: "myDragClass",
		dragHandle: "dragHandle"
	});

	function submitForm(action) {
		document.getElementById('action_name').value = action;
		var form = document.resultsForm;
		form.submit();
	}
{/literal}
</script>